home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / utilities / bin.lzh / bin / bin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-18  |  1.9 KB  |  118 lines

  1. /*
  2.  * bin.c - converts an AmigaDOS executable single-hunk file to a binary image
  3.  *
  4.  * Bruno Costa - 9 Dec 89 - 9 Dec 89
  5.  *
  6.  */
  7.  
  8. #include <exec/types.h>
  9. #include <proto/dos.h>
  10.  
  11. void print (char *msg);
  12. void copyright (void);
  13. void help (void);
  14. void notify (char *msg);
  15.  
  16. #define abort(x) {errnum = x; goto cleanup;}
  17.  
  18. void main (int argc, char *argv[])
  19. {
  20.  int errnum = 0;
  21.  BPTR f = NULL;
  22.  BPTR seglist = NULL;
  23.  
  24.  if (argc != 3)
  25.  {
  26.    copyright ();
  27.    help ();
  28.    abort (5);
  29.  }
  30.  else
  31.  {
  32.    seglist = LoadSeg (argv[1]);
  33.    if (!seglist)
  34.    {
  35.      notify ("Could not read executable file");
  36.      abort (20);
  37.    }
  38.    else
  39.    {
  40.      unsigned long *bin = NULL, *ptr = BADDR (seglist);
  41.  
  42.      do
  43.      {
  44.        if (ptr[-1])
  45.          if (bin)
  46.          {
  47.            notify ("Multiple-hunk load files not allowed");
  48.            abort (30);
  49.          }
  50.          else
  51.            bin = ptr;
  52.      } while (ptr = BADDR (ptr[0]));
  53.  
  54.      if (!bin)
  55.      {
  56.        notify ("All hunks are empty !");
  57.        abort (30);
  58.      }
  59.  
  60.      f = Open (argv[2], MODE_NEWFILE);
  61.      if (f)
  62.        Write (f, &bin[1], bin[-1] - 2 * sizeof (bin[0]));
  63.      else
  64.      {
  65.        notify ("Could not write binary file");
  66.        abort (20);
  67.      }
  68.    }
  69.  }
  70.  
  71.  copyright ();
  72.  
  73. cleanup:
  74.  
  75.  if (seglist)
  76.    UnLoadSeg (seglist);
  77.  if (f)
  78.    Close (f);
  79. }
  80.  
  81.  
  82. /*
  83.  * prints any message
  84.  */
  85. void print (char *msg)
  86. {
  87.  Write (Output(), msg, strlen (msg));
  88. }
  89.  
  90. /*
  91.  * prints a copyright message
  92.  */
  93. void copyright (void)
  94. {
  95.  print ("\x1b[33mbin v1.0\x1b[31m - \xa9 1989 by Bruno Costa\n");
  96. }
  97.  
  98. /*
  99.  * prints a help message
  100.  */
  101. void help (void)
  102. {
  103.  print ("Converts a relocatable code (or data) load file into a binary image file\n");
  104.  print ("usage: bin <executable> <binary>\n");
  105.  print ("\t <executable> = Single hunk AmigaDOS executable file\n");
  106.  print ("\t <binary> = Binary image output filename\n");
  107. }
  108.  
  109. /*
  110.  * notifies an error
  111.  */
  112. void notify (char *msg)
  113. {
  114.  print ("bin: ");
  115.  print (msg);
  116.  print ("\n");
  117. }
  118.